--
-- Simple battery indicator
--
-- Written by Tomas Lindquist Olsen, Sep 26 2010
--

-- some settings

local applet = {}
applet.h = 23

local wnd = false
local bar = false
local icon = false
local no_battery = true

local has_ibam = has_cmd("ibam");
local has_acpi = has_cmd("acpi");

-- this function does the actual work of finding the battery status
local function get_power_info()
    local r = {}
    r.percent = 0
    r.powersource = "ac"

    if has_ibam then
        local battery_status = get_cmd_output("ibam -a")
        --print("ibam output:\n"..battery_status)

        if string.find(battery_status, "No apm data", 1, true) == nil then
            no_battery = false
            local percent = string.match(battery_status, "Battery percentage:%s+(%d+) %%")
            assert(percent)
            r.percent = tonumber(percent)
        end
    end

    if has_acpi and not no_battery then
        local ac = get_cmd_output("acpi -a")
        local onac = string.match(ac, "on%-line")
        if onac == nil then
            r.powersource = "ac"
        else
            r.powersource = "battery"
        end
    end

    return r
end

-- this function creates the applet window
local function create_applet()
    -- we want to put it in the top right corner on the first monitor
    local monitor = Monitor:find(0,0)
    local top = 0
    local right = monitor.work:w()-1

    -- now we create a window for the applet
    -- 100x100 px
    wnd = Window:new(125,applet.h)
    --wnd:position(right-100, top)
    wnd:border(false)
    --wnd:box(NO_BOX)
    wnd:begin_group()

    -- and create the battery "bar"

    local bar1 = Widget:new(2,2,100,applet.h-4)
    bar1:box(ROUNDED_BOX)
    bar1:color(color(255,128,128))

    bar = Widget:new(2,2,50,applet.h-4)
    bar:box(ROUNDED_BOX)
    bar:color(color(128,255,128))

    -- also a place for the status icon
    icon = LuaWidget:new(105,3,16,16)
    icon:handle_callback(function(e)
        if e == PUSH and event_button() == 3 then
            local pm = PopupMenu(event_x_root(), event_y_root(), 100,1)
            pm:set_type(PopupMenu.POPUP1)
            pm:add("close applet")
            pm:popup()
            if pm:item() then
                wnd:destroy()
            end
            return true
        end
        return false
    end)

    -- and at last a label on top of them both
    -- local lbl = Widget:new(0,0,123,23)
    -- lbl:box(OFLAT_BOX)

    wnd:end_group()
    wnd:show()

    -- set the skip-taskbar wm hint
    wnd:wm_hint(SKIP_TASKBAR_HINT, HINT_ADD)
    wnd:wm_hint(STICKY_HINT, HINT_ADD)
end

-- this function registers the exit event
local function register_exit()
    -- by default a Window closes when it gets an Escape key event
    -- we override this to make it do so on Ctrl+'q' instead (after the widget has received focus)
    wnd:shortcut(CTRL + string.byte("q"))
    wnd:callback(function()
        local e = event()
        if e == KEY then
            wnd:destroy()
        end
    end)
end



local current_icon = ""
local function update_icon(charge, state)
    -- battery/battery\-(full|good|low|caution|empty}(\-charging)?\.png

    local which = ""
    local suf = ""

    if no_battery then
        which = "missing"
    else
        if charge > 90 then
            which = "full"
        elseif charge > 50 then
            which = "good"
        elseif charge > 10 then
            which = "low"
        elseif charge > 1 then
            which = "caution"
        else
            which = "empty"
        end

        if state == "ac" then
            suf = "-charging"
        end
    end

    local imgname = "battery/battery-"..which..suf..".png"

    if imgname ~= current_icon then
        --print("setting icon to: "..SharedImage:get_filename(imgname))
        local img = pngImage:get(imgname)
        icon:box(img)
        current_icon = imgname
    end
end

local function timer_cb(t)
    local p = get_power_info()
    bar:label(p.percent.."%")

    local sz = math.max(p.percent,40)
    bar:resize(sz,applet.h-4)

    update_icon(p.percent,state)
    wnd:redraw()

    t:readd(60)
end

create_applet()
register_exit()
Timer(timer_cb):add(0)

print("battery applet complete")
